package com.cgtong.android.mvp.model;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.ConstantManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.mvp.contract.RecycleListContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by chunc on 2018/1/3.
 */

public class RecycleListModel implements RecycleListContract.Model {
    @Override
    public void httpRecycleList(long maxId,String searchKey,long userId,final BaseCallBack<List<ProblemBean>> baseCallBack) {
        EasyHttp.post(ApiManager.HTTP_RECYCLE_PROBLEMS_URL)
                .accessToken(true)
                .params("maxId",String.valueOf(maxId))
                .params("pageSize",String.valueOf(ConstantManager.MY_REPORTS_PAGE_SIZE))
                .params("userId",String.valueOf(userId))
                .params("searchtxt",searchKey)
                .execute(new CallBackProxy<ApiResult<List<ProblemBean>>, List<ProblemBean>>(new SimpleCallBack<List<ProblemBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(List<ProblemBean> problemBeen) {
                        baseCallBack.onSuccess(problemBeen);
                    }
                }) {
                });
    }

    @Override
    public void commitRecycleProblemIDs(String ids, final BaseCallBack<ApiResult> baseCallBack) {
        EasyHttp.post(ApiManager.COMMIT_RECYCLE_IDS_URL)
                .params("ids", ids)//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            baseCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void deleteRecycles(String ids, final BaseCallBack<ApiResult> baseCallBack) {
        EasyHttp.post(ApiManager.DELETE_RECYCLE_IDS_URL)
                .params("ids", ids)//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            baseCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
